package win.smartown.kotlin.grammar

/**
 * Created by Smartown(<a href='https://github.com/smartown'>@Smartown</a>) on 2017/6/4.
 *
 * 使用可空变量以及控制检查
 *
 * 当空值可能出现时应该明确指出该引用可空。
 */
fun main(args: Array<String>) {
    printProduct("4", "5")
    printProduct("a", "5")
    printProduct("4", "b")
    printProduct(arg2 = "4", arg1 = "8")
}

//当str中不包含整数时返回空
fun parseInt(str: String): Int? {
    try {
        return str.toInt()
    } catch (e: Exception) {

    }
    return null
}

fun printProduct(arg1: String, arg2: String) {
    val x = parseInt(arg1)
    val y = parseInt(arg2)

    if (x == null) {
        println("Wrong number format in arg1:\"$arg1\"")
        return
    }
    if (y == null) {
        println("Wrong number format in arg2:\"$arg2\"")
        return
    }
    println("arg1:$arg1 * arg2:$arg2 = " + x * y)
}